﻿using DataShow.codeModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DataShow.ascx
{
    public partial class AddPanel : System.Web.UI.UserControl
    {
        public string StationCode { get; set; }
        //private PanelItem panel;
        private PanelItem Panel;
        public PanelItem panel
        {
            get
            {
                object o = ViewState["PanelItem"];
                if (o != null)
                {
                    return o as PanelItem;
                }
                else
                {
                    return Panel;
                }
            }
            set
            {
                ViewState["PanelItem"] = value;
                this.Panel = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 0; i < this.Controls.Count; i++)
                {
                    if (this.Controls[i] is DropDownList)
                    {
                        BindData(this.Controls[i] as DropDownList);
                    }
                }
                panel = new PanelItem(StationCode);
            }
        }

        #region 获得控件上用户填写的值
        /// <summary>
        /// 获得控件上用户填写的值
        /// </summary>
        /// <returns></returns>
        public PanelItem GetPanelValue()
        {
            var FD00_24 = panel.Values.Where(x => x.Key == "FD00_24").FirstOrDefault();
            var FD24_48 = panel.Values.Where(x => x.Key == "FD24_48").FirstOrDefault();
            var FD48_72 = panel.Values.Where(x => x.Key == "FD48_72").FirstOrDefault();
            var FD72_96 = panel.Values.Where(x => x.Key == "FD72_96").FirstOrDefault();
            var FD96_120 = panel.Values.Where(x => x.Key == "FD96_120").FirstOrDefault();

            FD00_24.Value.Add(ddl_00_24_F12.SelectedValue);
            FD00_24.Value.Add(ddl_00_24_B12.SelectedValue);
            FD00_24.Value.Add(nud_TMAX_00_24.GetNumber().ToString());
            FD00_24.Value.Add(nud_TMIN_00_24.GetNumber().ToString());

            FD24_48.Value.Add(ddl_24_48_F12.SelectedValue);
            FD24_48.Value.Add(ddl_24_48_B12.SelectedValue);
            FD24_48.Value.Add(nud_TMAX_24_48.GetNumber().ToString());
            FD24_48.Value.Add(nud_TMIN_24_48.GetNumber().ToString());

            FD48_72.Value.Add(ddl_48_72_F12.SelectedValue);
            FD48_72.Value.Add(ddl_48_72_B12.SelectedValue);
            FD48_72.Value.Add(nud_TMAX_48_72.GetNumber().ToString());
            FD48_72.Value.Add(nud_TMIN_48_72.GetNumber().ToString());

            FD72_96.Value.Add(ddl_72_96_F12.SelectedValue);
            FD72_96.Value.Add(ddl_72_96_B12.SelectedValue);
            FD72_96.Value.Add(nud_TMAX_72_96.GetNumber().ToString());
            FD72_96.Value.Add(nud_TMIN_72_96.GetNumber().ToString());

            FD96_120.Value.Add(ddl_96_120_F12.SelectedValue);
            FD96_120.Value.Add(ddl_96_120_B12.SelectedValue);
            FD96_120.Value.Add(nud_TMAX_96_120.GetNumber().ToString());
            FD96_120.Value.Add(nud_TMIN_96_120.GetNumber().ToString());

            return panel;
        } 
        #endregion

        private void BindData(DropDownList ddl)
        {
            DataTable dt = new codeDAL.Common().GetAllWeathers();
            ddl.DataSource = dt;
            ddl.DataTextField = "text";
            ddl.DataValueField = "id";
            ddl.DataBind();
        }

        public void SetValue(codeModel.PanelItem item)
        {
            ddl_00_24_F12.SelectedValue = FilterStation(item.Values["FD00_24"][0]);
            ddl_00_24_B12.SelectedValue = FilterStation(item.Values["FD00_24"][1]);
            nud_TMAX_00_24.SetNumber(FilterStation(item.Values["FD00_24"][2]));
            nud_TMIN_00_24.SetNumber(FilterStation(item.Values["FD00_24"][3]));

            ddl_24_48_F12.SelectedValue = FilterStation(item.Values["FD24_48"][0]);
            ddl_24_48_B12.SelectedValue = FilterStation(item.Values["FD24_48"][1]);
            nud_TMAX_24_48.SetNumber(FilterStation(item.Values["FD24_48"][2]));
            nud_TMIN_24_48.SetNumber(FilterStation(item.Values["FD24_48"][3]));

            ddl_48_72_F12.SelectedValue = FilterStation(item.Values["FD48_72"][0]);
            ddl_48_72_B12.SelectedValue = FilterStation(item.Values["FD48_72"][1]);
            nud_TMAX_48_72.SetNumber(FilterStation(item.Values["FD48_72"][2]));
            nud_TMIN_48_72.SetNumber(FilterStation(item.Values["FD48_72"][3]));


            ddl_72_96_F12.SelectedValue = FilterStation(item.Values["FD72_96"][0]);
            ddl_72_96_B12.SelectedValue = FilterStation(item.Values["FD72_96"][1]);
            nud_TMAX_72_96.SetNumber(FilterStation(item.Values["FD72_96"][2]));
            nud_TMIN_72_96.SetNumber(FilterStation(item.Values["FD72_96"][3]));

            ddl_96_120_F12.SelectedValue = FilterStation(item.Values["FD96_120"][0]);
            ddl_96_120_B12.SelectedValue = FilterStation(item.Values["FD96_120"][1]);
            nud_TMAX_96_120.SetNumber(FilterStation(item.Values["FD96_120"][2]));
            nud_TMIN_96_120.SetNumber(FilterStation(item.Values["FD96_120"][3]));
        }

        /// <summary>
        /// 用于同步数据
        /// </summary>
        /// <param name="item"></param>
        public void SyncValue(codeModel.PanelItem item)
        {
            
        }

        /// <summary>
        /// 过滤站点
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private string FilterStation(string values)
        {
            string[] stationValues = values.Split('|');
            var getvalue = stationValues.Where(x => x.Contains(StationCode)).FirstOrDefault();
            if (getvalue != null)
            {
                return getvalue.Split(';')[1];
            }
            else
            {
                return "0";
            }
        }
    }
}